package com.bestcem.xm.txtanls.controller.convert;

import com.bestcem.xm.txtanls.controller.vo.chart.*;
import com.bestcem.xm.txtanls.service.dto.chart.*;
import com.bestcem.xm.txtanls.util.ChartUtil;
import org.springframework.beans.BeanUtils;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * DTO到VO转换类
 * @author jy.zhao
 * @version 2.0
 * @date 2021/8/5 22:17
 **/
public class ChartConvert {

    /**
     * 总体情感细分emotionDetailDTO转换为EmotionDetailVO
     * @param emotionDetailDTO
     * @param interval
     * @return EmotionDetailVO
     * @author jy.zhao
     * @date 2021/8/5 22:24
     **/
    public static EmotionDetailVO emotionDetailDTOToVO(EmotionDetailDTO emotionDetailDTO, Integer interval) {
        EmotionDetailVO emotionDetailVO = new EmotionDetailVO();
        emotionDetailVO.setCurrentTime(ChartUtil.formatPeriodTime(emotionDetailDTO.getCurPeriod(), interval));
        emotionDetailVO.setLastTime(ChartUtil.formatPeriodTime(emotionDetailDTO.getLastPeriod(), interval));
        emotionDetailVO.setCurrentPeriod(emotionDTOToVO(emotionDetailDTO.getCurEmotion()));
        emotionDetailVO.setRingRatio(emotionDTOToVO(emotionDetailDTO.getGrowthRatio()));
        emotionDetailVO.setRingGrowth(emotionDTOToVO(emotionDetailDTO.getGrowth()));
        return emotionDetailVO;
    }

    /**
     * 总体情感细分 emotionDTO 转换为emotionVO
     * @param emotionDTO
     * @return EmotionVO
     * @author jy.zhao
     * @date 2021/8/5 22:26
     **/
    private static EmotionVO emotionDTOToVO(EmotionDTO emotionDTO) {
        EmotionVO emotionVO = new EmotionVO();
        BeanUtils.copyProperties(emotionDTO, emotionVO);
        return emotionVO;
    }

    /**
     * 总体情感趋势 emotionTrendDTO 转换为emotionVO
     * @param emotionDTOList
     * @return List<EmotionTrendVO>
     * @author jy.zhao
     * @date 2021/8/5 22:35
     **/
    public static List<EmotionTrendVO> emotionDTOToVOList(List<EmotionDTO> emotionDTOList, Integer interval) {
        List<EmotionTrendVO> results = new ArrayList<>();
        for(EmotionDTO emotionDTO:emotionDTOList) {
            EmotionTrendVO emotionTrendVO = new EmotionTrendVO();
            emotionTrendVO.setMixture(emotionDTO.getMixture());
            emotionTrendVO.setNegative(emotionDTO.getNegative());
            emotionTrendVO.setNeutral(emotionDTO.getNeutral());
            emotionTrendVO.setPositive(emotionDTO.getPositive());
            emotionTrendVO.setTotal(emotionDTO.getTotal());
            emotionTrendVO.setTime(ChartUtil.formatPeriodTime(emotionDTO.getPeriodTime(), interval));
            results.add(emotionTrendVO);
        }
        return results;
    }

    /**
     * 观点组统计DTO转化为VO
     * @param groupStatDTOList
     * @return List<ViewpointGroupStatVO>
     * @author jy.zhao
     * @date 2021/8/6 10:41
     **/
    public static List<ViewpointGroupStatVO> ViewpointGroupStatDTOToVOList(List<ViewpointGroupStatDTO> groupStatDTOList) {
        List<ViewpointGroupStatVO> results = new ArrayList<>();
        for(ViewpointGroupStatDTO groupStatDTO:groupStatDTOList) {
            ViewpointGroupStatVO groupStatVO = new ViewpointGroupStatVO();
            BeanUtils.copyProperties(groupStatDTO.getViewpointGroupStat(), groupStatVO);
            List<ViewpointStatVO> viewpointStatVOList = new ArrayList<>();
            for(ViewpointStatDTO viewpointStatDTO:groupStatDTO.getViewpointStats()) {
                ViewpointStatVO viewpointStatVO = new ViewpointStatVO();
                BeanUtils.copyProperties(viewpointStatDTO, viewpointStatVO);
                viewpointStatVOList.add(viewpointStatVO);
            }
            groupStatVO.setViewpointStatVOList(viewpointStatVOList);
            results.add(groupStatVO);
        }
        return results;
    }

    /**
     * TopicAnalysisDTO 转为 TopicAnalysisVO
     * @param dto TopicAnalysisDTO
     * @return TopicAnalysisVO TopicAnalysisVO
     * @author jy.zhao
     * @date 2021/11/17 15:11
     **/
    public static TopicAnalysisVO topicAnalysisDTOToVO(TopicAnalysisDTO dto) {
        TopicAnalysisVO vo = new TopicAnalysisVO();
        List<TopicTopDTO> topicTopDTOList = dto.getTopicTopDTOList();
        TopicHotDTO topicFastDTO = dto.getTopicFastDTO();
        TopicHotDTO topicHotDTO = dto.getTopicHotDTO();
        TopicHotDTO topicHotLastDTO = dto.getTopicHotLastDTO();

        List<TopicTopVO> topicTopVOList = new ArrayList<>();
        for(TopicTopDTO topicTopDTO:topicTopDTOList) {
            TopicTopVO topicTopVO = new TopicTopVO();
            topicTopVO.setName(topicTopDTO.getName());
            topicTopVO.setRatio(topicTopDTO.getRatio());
            topicTopVOList.add(topicTopVO);
        }
        // 热门话题
        TopicHotVO topicHotVO = new TopicHotVO();
        topicHotVO.setName(topicHotDTO.getName());
        topicHotVO.setCount(topicHotDTO.getCurCount());
        topicHotVO.setRingRatio(topicHotDTO.getRingRatio());
        // 热度激增话题
        TopicHotVO topicFastVO = new TopicHotVO();
        topicFastVO.setName(topicFastDTO.getName());
        topicFastVO.setCount(topicFastDTO.getCurCount());
        topicFastVO.setRingRatio(topicFastDTO.getRingRatio());
        // 上期热门话题
        TopicHotLastVO topicHotLastVO = new TopicHotLastVO();
        topicHotLastVO.setName(topicHotLastDTO.getName());
        topicHotLastVO.setCurCount(topicHotLastDTO.getCurCount());
        topicHotLastVO.setLastCount(topicHotLastDTO.getLastCount());
        vo.setTopicFastVO(topicFastVO);
        vo.setTopicTopVOList(topicTopVOList);
        vo.setTopicHotLastVO(topicHotLastVO);
        vo.setTopicHotVO(topicHotVO);
        return vo;
    }

    /**
     * 话题提及率变化 TopicReferenceRateDTO转为 TopicReferenceRateVO
     * @param dto TopicReferenceRateDTO
     * @param interval
     * @return TopicReferenceRateVO
     * @author jy.zhao
     * @date 2021/8/5 23:07
     **/
    public static TopicReferenceRateVO topicReferenceRateDTOToVO(TopicReferenceRateDTO dto, Integer interval) {
        TopicReferenceRateVO vo = new TopicReferenceRateVO();
        vo.setCurrentTime(ChartUtil.formatPeriodTime(dto.getCurPeriod(), interval));
        vo.setLastTime(ChartUtil.formatPeriodTime(dto.getLastPeriod(), interval));
        List<ReferenceRateDTO> refRates = dto.getRefRates();
        List<ReferenceRateVO> referenceRateVOList = new ArrayList<>();
        for(ReferenceRateDTO referenceRateDTO:refRates) {
            ReferenceRateVO referenceRateVO = new ReferenceRateVO();
            referenceRateVO.setId(referenceRateDTO.getId());
            referenceRateVO.setName(referenceRateDTO.getName());
            referenceRateVO.setCurRatio(referenceRateDTO.getCurRatio().setScale(0, RoundingMode.HALF_UP).intValue());
            referenceRateVO.setLastRatio(referenceRateDTO.getLastRatio().setScale(0, RoundingMode.HALF_UP).intValue());
            referenceRateVO.setGrowthRatio(referenceRateVO.getCurRatio() - referenceRateVO.getLastRatio());
            referenceRateVOList.add(referenceRateVO);
        }
        vo.setList(referenceRateVOList);
        return vo;
    }

    /**
     * 观点评价变化 ViewPointEvaluateDTO转换为ViewPointEvaluateVO
     * @param dto
     * @param interval
     * @return ViewPointEvaluateVO
     * @author jy.zhao
     * @date 2021/8/5 23:08 
     **/
    public static ViewPointEvaluateVO ViewPointEvaluateDTOToVO(ViewPointEvaluateDTO dto, Integer interval) {
        ViewPointEvaluateVO vo = new ViewPointEvaluateVO();
        vo.setCurrentTime(ChartUtil.formatPeriodTime(dto.getCurPeriod(), interval));
        vo.setLastTime(ChartUtil.formatPeriodTime(dto.getLastPeriod(), interval));
        List<ViewPointEvaluateRatioDTO> refRates = dto.getViewRatios();
        List<ViewPointEvaluateRatioVO> viewPointEvaluateRatioVOList = new ArrayList<>();
        for(ViewPointEvaluateRatioDTO viewPointEvaluateRatioDTO:refRates) {
            ViewPointEvaluateRatioVO viewPointEvaluateRatioVO = new ViewPointEvaluateRatioVO();
            BeanUtils.copyProperties(viewPointEvaluateRatioDTO, viewPointEvaluateRatioVO);
            viewPointEvaluateRatioVOList.add(viewPointEvaluateRatioVO);
        }
        vo.setList(viewPointEvaluateRatioVOList);
        return vo;
    }

    /**
     * 词云图 workCloudDataDTOList转为WorkCloudVOList
     * @param workCloudDataDTOList
     * @return List<WorkCloudVO>
     * @author jy.zhao
     * @date 2021/8/5 23:13
     **/
    public static List<WorkCloudVO> workCloudDataDTOToVOList(List<WorkCloudDataDTO> workCloudDataDTOList) {
        List<WorkCloudVO> results = new ArrayList<>();
        for(WorkCloudDataDTO workCloudDataDTO:workCloudDataDTOList) {
            WorkCloudVO workCloudVO = new WorkCloudVO();
            BeanUtils.copyProperties(workCloudDataDTO, workCloudVO);
            results.add(workCloudVO);
        }
        return results;
    }

    /**
     * 话题关联转换 topicRelationDTOList转换为TopicRelationVOList
     * @param topicRelationDTOList
     * @return List<TopicRelationVO>
     * @author jy.zhao
     * @date 2021/8/5 23:17
     **/
    public static List<TopicRelationVO> topicRelationDTOToVOList(List<TopicRelationDTO> topicRelationDTOList) {
        List<TopicRelationVO> results = new ArrayList<>();
        for(TopicRelationDTO topicRelationDTO:topicRelationDTOList) {
            TopicRelationVO topicRelationVO = new TopicRelationVO();
            BeanUtils.copyProperties(topicRelationDTO, topicRelationVO);
            results.add(topicRelationVO);
        }
        return results;
    }

    /**
     * 话题提及率变化趋势转换VO
     * @param topicStatDTOList
     * @param interval
     * @return List<TopicStatVO>
     * @author jy.zhao
     * @date 2021/8/6 10:33 
     **/
    public static List<TopicStatVO> topicStatDTOToVOList(List<TopicStatDTO> topicStatDTOList, Integer interval) {
        List<TopicStatVO> results = new ArrayList<>();
        for(TopicStatDTO topicStatDTO:topicStatDTOList) {
            TopicStatVO topicStatVO = new TopicStatVO();
            topicStatVO.setRatio(topicStatDTO.getRatio());
            topicStatVO.setPeriodTime(ChartUtil.formatPeriodTime(topicStatDTO.getPeriodTime(), interval));
            results.add(topicStatVO);
        }
        return results;
    }

    /**
     * 观点情感分布变化
     * @param dtoList
     * @param interval
     * @return List<ViewpointEvaluateStatVO>
     * @author jy.zhao
     * @date 2021/8/6 10:37
     **/
    public static List<ViewpointEvaluateStatVO> viewpointEvaluateStatDTOToVOList(List<ViewpointEvaluateStatDTO> dtoList, Integer interval) {
        List<ViewpointEvaluateStatVO> results = new ArrayList<>();
        for(ViewpointEvaluateStatDTO dto:dtoList) {
            ViewpointEvaluateStatVO vo = new ViewpointEvaluateStatVO();
            vo.setPositive(dto.getPositive());
            vo.setNegative(dto.getNegative());
            vo.setNeutral(dto.getNeutral());
            vo.setMix(dto.getMix());
            vo.setPeriodTime(ChartUtil.formatPeriodTime(dto.getPeriodTime(), interval));
            results.add(vo);
        }
        return results;
    }
}
